package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQuery;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.DateUtil;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.XlsMapping;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/view")
    public String employee(Model model) {

        return "system/employee";
    }

    @RequiresPermissions(value = {"employee:query", "员工列表"}, logical = Logical.OR)
    @RequestMapping("/query")
    @ResponseBody
    public Object query(EmployeeQuery qo) {
        return employeeService.query(qo);
    }

    // 验证用户名
    @RequestMapping("/validateName")
    @ResponseBody
    public Object validateName(String name,Long id) {

        return employeeService.validateUsernname(name, id);
    }

    @RequiresPermissions(value = {"employee:save", "员工新增/编辑"}, logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Employee entity) {
        try {
            employeeService.saveOrUpdate(entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            return new JsonResult().mark(ex.getMessage());
        }
        return new JsonResult();
    }

    @RequiresPermissions(value = {"employee:changeState", "员工离职/复职"}, logical = Logical.OR)
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(Long id) {
        try {
            employeeService.changeState(id);
        } catch (Exception ex) {
            return new JsonResult().mark(ex.getMessage());
        }
        return new JsonResult();
    }

    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        // 设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        // 创建 xls 文件
        Workbook wb = new HSSFWorkbook();

        // 创建工作簿
        Sheet sheet = wb.createSheet();

        // 获取 数据
        List<Employee> list = employeeService.listAll();
        // 设置 标题
        Row titleRow = sheet.createRow(0);
        for (int i = 0; i < XlsMapping.EMPLOYEE_MAP.length; i++) {
            titleRow.createCell(i).setCellValue(XlsMapping.EMPLOYEE_MAP[i][0]);
        }
        // 设置 值
        for (int i = 0; i < list.size(); i++) {
            Row row = sheet.createRow(i + 1);
            for (int x = 0; x < XlsMapping.EMPLOYEE_MAP.length; x++) {
                Object value = XlsMapping.getValue(list.get(i), 1, XlsMapping.EMPLOYEE_MAP[x]);
                if (value == null) continue;
                if (Date.class.isInstance(value)) {
                    row.createCell(x).setCellValue(DateUtil.parseDate((Date) value));
                } else {
                    row.createCell(x).setCellValue(value.toString());
                }
            }
        }
        // 响应文件
        wb.write(response.getOutputStream());
    }

    @RequestMapping("/uploadXls")
    public void uploadXls(MultipartFile file) {
        System.out.println(123);
        return;
    }

    //查询所有员工对象
    @RequestMapping("/selectAll")
    @ResponseBody
    public Object selectAll(){
        return employeeService.listAll();
    }


    @RequestMapping("/selectName")
    @ResponseBody
    public List<Map<String,Object>> selectName(){
        return employeeService.selectName();
    }
}
